﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Data.SqlServerCe;

namespace MercuryFE
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            // confirm we have the sql server ce library
            try
            {
                SqlCeEngine sqlCheck = new SqlCeEngine();
            }
            catch (Exception e)
            {
                MessageBox.Show("ERROR:" + e.Message + Environment.NewLine + e.Source, "Error initiating Sql Server CE engine", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // now create new db, if required
            checkIfDBExists();
            

            Application.Run(new frmConfig());
            //Application.Run(new KeyMapper(new Dictionary<string, string> {{"A","B"} }));
        }


        private static bool checkIfDBExists()
        {
            string connectionString = LauncherLib.getDBConnection();
            // in format Data Source = [file]
            string connFile =  (connectionString.Split(';'))[0];

            string dbFile = (connFile.Split('='))[1];
            dbFile = dbFile.Replace(@"|DataDirectory|\","");

            if (File.Exists(dbFile))
                return true;
            else
            {

                using (SqlCeEngine en = new SqlCeEngine(connectionString))
                {

                    en.CreateDatabase();
                    LauncherLib.executeDBScript(false);
                }
                
                return false;
            }
        }
    }
}
